/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Layer which includes the logo-sprites
 * Also initializes the Common-module
 */

#include "SplashLayer.h"
#include "SplashScene.h"
#include "../MainEngine.h"
#include "../Common.h"
#include "../Resources.h"

using namespace cocos2d;

// rotation speed for the icon
const float ICON_RPM = 1;

// default constructor
SplashLayer::SplashLayer( CCScene *scene ) : mMaster( scene ), mUnitSprite( 0 ), mLoadingIcon( 0 ), mLayerTimePassed( 0 ) {
	initLayer();
}

// default destructor
SplashLayer::~SplashLayer() {}

// initializes this layer
void SplashLayer::initLayer() {
	// disable input
	setIsTouchEnabled( false );

	// get window size
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
	// initialize common-module
	initValues( screenSize.width, screenSize.height );

	// create sprites
	mBackground = CCSprite::spriteWithFile( getResource( Textures::GAME_BACKGROUND ) );
	mUnitSprite = CCSprite::spriteWithFile( getResource( Textures::LOADING_UNIT ) );
	mLoadingIcon = CCSprite::spriteWithFile( getResource( Textures::LOADING_ICON ) );

	// set background sprite pos, scale etc
	mBackground->setScaleX( RATEX );
	mBackground->setScaleY( RATEY );
	mBackground->setPosition( ccp( WIN_WIDTH * 0.5f, WIN_HEIGHT * 0.5f ) );

	// set unit sprite positions, scales etc
	mUnitSprite->setPosition( ccp( WIN_WIDTH * 0.65f, WIN_HEIGHT * 0.5f ) );
	mUnitSprite->setScaleX( RATEX );
	mUnitSprite->setScaleY( RATEY );

	// note that as we want the loading icon to keep it's circle shape, 
	// we may only set the global scale instead of scaling x and y separately
	mLoadingIcon->setPosition( ccp( WIN_WIDTH * 0.35f, WIN_HEIGHT * 0.4f ) );
	mLoadingIcon->setScale( RATEX * 2.0f );

	mLoadingLabel = CCLabelBMFont::labelWithString( "Loading...", getResource( Fonts::FONT_NORMAL ) );
	mLoadingLabel->setScaleX( RATEX );
	mLoadingLabel->setScaleY( RATEY );
	mLoadingLabel->setPosition( ccp( WIN_WIDTH * 0.35f, WIN_HEIGHT * 0.6f ) );

	// add sprites as childs for this layer
	addChild( mBackground );
	addChild( mUnitSprite );
	addChild( mLoadingIcon );
	addChild( mLoadingLabel );
}

// update the layer
void SplashLayer::update( cocos2d::ccTime dt ) {
	// increase time
	mLayerTimePassed += dt;
	// set icon rotation
	mLoadingIcon->setRotation( mLayerTimePassed * ICON_RPM * 360.0f );
}